package cn.icanci.loopstack.plugin.dalgen.engine.impl;

import cn.icanci.loopstack.plugin.dalgen.config.GlobalConfig;
import cn.icanci.loopstack.plugin.dalgen.factory.ClassInfoFactory;
import cn.icanci.loopstack.plugin.dalgen.intercept.CustomEngine;
import cn.icanci.loopstack.plugin.dalgen.model.ConfigurationInfoVO;

import java.util.ServiceLoader;
import java.util.Set;

import com.google.common.collect.Sets;

/**
 * CustomEngineProxy 基于自定义拦截接口的方法调用
 *
 * @author icanci
 * @since 1.0 Created in 2022/05/13 18:52
 */
public class CustomEngineProxy {

    /**
     * 处理
     */
    public static void handleCustom() {
        Set<CustomEngine> classes = toConsumers();
        System.out.println("CustomEngineProxy#handleCustom classes:" + classes);
        for (CustomEngine clazz : classes) {
            // 基于配置项检测是否需要启用自定义实现类
            ConfigurationInfoVO gobleConfig = GlobalConfig.getGobleConfig();
            if ("*;".equals(gobleConfig.getCustomHandleInclude()) || "*".equals(gobleConfig.getCustomHandleInclude())
                || gobleConfig.getCustomHandleIncludeMap().containsKey(clazz.getClass().getSimpleName())) {
                clazz.handle(gobleConfig, ClassInfoFactory.getClassInfoList());
            }
        }
    }

    /***
     * 扫描全包获取 实现CustomEngine接口的类
     */
    private static Set<CustomEngine> toConsumers() {
        ServiceLoader<CustomEngine> loader = ServiceLoader.load(CustomEngine.class);
        Set<CustomEngine> ret = Sets.newHashSet();
        for (CustomEngine customEngine : loader) {
            ret.add(customEngine);
        }
        return ret;
    }

}
